﻿using AuthCenter.Api.Read.Application.Command.User;
using AuthCenter.Api.Read.Application.Output;
using AuthCenter.Application;
using AuthCenter.Domain;
using AuthCenter.ErrorCode;
using AutoMapper;
using MediatR;
using static System.Runtime.InteropServices.JavaScript.JSType;

namespace AuthCenter.Api.Read.Application.Hander.User
{
    public class GetUserShowHander:IRequestHandler<GetUserShowCommand,ApiResult<List<UserOutput>>>
    {
        private readonly IBaseRepository<UserModel> _userRepository;
        private readonly IBaseRepository<ProvinceModel> _provinceRepository;
        private readonly IMapper _mapper;
        private readonly IBaseRepository<CityModel> _cityRepository;
        private readonly IBaseRepository<CountyModel> _countyRepository;
        private readonly IBaseRepository<RoleModel> _roleRepository;
        private readonly IBaseRepository<UserRoleModel> _userRoleRepository;
        public GetUserShowHander(IBaseRepository<UserModel> userRepository, IMapper mapper, IBaseRepository<ProvinceModel> provinceRepository, IBaseRepository<CityModel> cityRepository, IBaseRepository<CountyModel> countyRepository, IBaseRepository<RoleModel> roleRepository, IBaseRepository<UserRoleModel> userRoleRepository)
        {
            _userRepository = userRepository;
            _mapper = mapper;
            _provinceRepository = provinceRepository;
            _cityRepository = cityRepository;
            _countyRepository = countyRepository;
            _roleRepository = roleRepository;
            _userRoleRepository = userRoleRepository;
        }
        /// <summary>
        /// 显示
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public Task<ApiResult<List<UserOutput>>> Handle(GetUserShowCommand request, CancellationToken cancellationToken)
        {
            ApiResult<List<UserOutput>> res = new ApiResult<List<UserOutput>>();
            var user = _userRepository.GetAll().OrderByDescending(x => x.Id).Where(x => x.IsDeleted == false);


            //将数据通过AutoMapper映射到UsersRoleDTO中
            IList<UserOutput> userDTO = _mapper.Map<IList<UserOutput>>(user);
            foreach (var item in userDTO)
            {
                //根据用户编号获取满足条件的用户角色信息
                var userroleinfo = _userRoleRepository.GetAll().Where(x => x.IsDeleted == false && x.UserId == item.Id);
                //获取所有角色信息
                var roleinfo = _roleRepository.GetAll().Where(x => x.IsDeleted == false);

                //通过Ling关联用户、角色
                var query = (from a in userroleinfo
                             join b in roleinfo on a.RoleId equals b.Id
                             select new { a, b }).ToList();

                //从数据集中单独获取角色名称信息
                List<string> roleName = query.Select(x => x.b.RoleName).ToList();

                //Join()为字符串连接方法  参数1代表分隔符 参数2代表需要操作的数组
                item.RoleName = string.Join(",", roleName);
            }
            res.Code = ApiEnums.OK;
            res.Message = "显示成功";
            res.Data = userDTO.ToList();
            return Task.FromResult(res);
        }
    }
}
